<template>  
    <div class=countdown>  
       <h3>{{ count }}</h3> 

        
       

    </div>  
  </template>  

<script>
import {getLastEvent} from '@/api/event'


export default{

    // props:["room_id"],
    data(){
        return {
            count: '', //倒计时
            isNotAble:'',
            event:{},
        //    remainSeconds: '',}
           timer:null,
           order:{},
           captchaText:"",
           showCaptcha:true,
           
        }
    },
    mounted(){
        getLastEvent().then(res=>{
            try{
                this.$nextTick(function(){this.$root.Bus.$emit('isNotAble',true)})
             if(res.code==200)
               { this.event=res.data
                this.Time();}
            else{ this.count=res.msg
            this.$nextTick(function(){this.$root.Bus.$emit('isNotAble',true)})
            }
        }catch{
                this.$message.error("获取失败")
            }
        })
        //调用定时器
    },
    methods: {
        bookRoom(){
            this.showCaptcha=true;
        },
        
        countDown() {
            if (this.event.remainSeconds > 0) {
                this.event.remainSeconds -= 1
        this.isNotAble=true;
        let d = parseInt(this.event.remainSeconds / (24 * 60 * 60))
        d = d < 10 ? "0" + d : d
        let h = parseInt(this.event.remainSeconds / (60 * 60) % 24);
        h = h < 10 ? "0" + h : h
        let m = parseInt(this.event.remainSeconds / 60 % 60);
        m = m < 10 ? "0" + m : m
        let s = parseInt(this.event.remainSeconds % 60);
        s = s < 10 ? "0" + s : s
        this.count = "离"+this.event.event_name+"开始还有:\n"+d + '天' + h + '时' + m + '分' + s + '秒'
        this.$nextTick(function(){this.$root.Bus.$emit('isNotAble',true)})
    }
    else if (this.event.remainSeconds == 0){
        clearInterval(this.timer)
        //this.isNotAble=false;
        // this.count="选宿舍进行中"
       this.count = this.event.event_name+'进行中'
       this.$nextTick(function(){this.$root.Bus.$emit('isNotAble',false)})
    }
    else if(this.event.remainSeconds <0){
        this.isNotAble=true;
        // this.count="选宿舍进行中"
       this.count = '选宿舍已结束' 
       this.$nextTick(function(){this.$root.Bus.$emit('isNotAble',true)})
    }

    },
    Time() {
        this.timer=setInterval(() => {
            this.countDown()
        }, 1000)
     
    },
    }
}

</script>
<style scoped>
.countdown{
    position: fixed;
    right: 20px;
    bottom: 20px;
    /* height: 100px; */
    border-radius: 5%;
    border: dotted 1px;

    width: 200px;
    justify-content: center;
    z-index: 999;
    /* background-color: green; */
}

</style>